package cn.iocoder.yudao.module.biz.service.htPlanDetailExe;

import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentroubleannexs.HtHiddentroubleannexsDO;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlan.HtPlanDO;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlanDetailexeannexs.HtPlanDetailexeannexsDO;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlanExe.HtPlanExeDO;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlan.HtPlanMapper;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlanDetailexeannexs.HtPlanDetailexeannexsMapper;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlanExe.HtPlanExeMapper;
import cn.iocoder.yudao.module.infra.dal.dataobject.oss.OssDO;
import cn.iocoder.yudao.module.infra.dal.mysql.oss.OssMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.htPlanDetailExe.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlanDetailExe.HtPlanDetailExeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.htPlanDetailExe.HtPlanDetailExeConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlanDetailExe.HtPlanDetailExeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 排查项目计划执行结果 Service 实现类
 *
 * @author yudao
 */
@Service
@Validated
public class HtPlanDetailExeServiceImpl implements HtPlanDetailExeService {

    @Resource
    private HtPlanDetailExeMapper htPlanDetailExeMapper;
    @Resource
    private HtPlanExeMapper htPlanExeMapper;
    @Resource
    private HtPlanMapper htPlanMapper;
    @Resource
    private HtPlanDetailexeannexsMapper htPlanDetailexeannexsMapper;
    @Resource
    private OssMapper ossMapper;

    private DateFormat timeFormat=new SimpleDateFormat("yyyy-MM-dd 00:00:00");

    @Override
    public String createHtPlanDetailExe(HtPlanDetailExeCreateReqVO createReqVO) throws ParseException {
        // 插入
        HtPlanDetailExeDO htPlanDetailExe = HtPlanDetailExeConvert.INSTANCE.convert(createReqVO);
        htPlanDetailExeMapper.insert(htPlanDetailExe);

        //本次计划执行排查任务全部执行完毕，排查计划执行的状态设置为”已执行“
        //计算本次待执行的计划 应该排查项目与已排查项目的数量是否相等，相等时设置排查计划执行的状态为"已执行"
        int syDif=htPlanExeMapper.countDifference(createReqVO.getPlanId(),createReqVO.getPlanExeId());
        if(syDif==0) {
            HtPlanExeDO planExeDO = new HtPlanExeDO();
            planExeDO.setId(createReqVO.getPlanExeId());
            planExeDO.setExeState("1");
            htPlanExeMapper.updateById(planExeDO);
        }

        //排查计划的排查项目,最后截止日期-系统日期=1，所有的排查计划执行状态exeState都为已执行,设置排查计划的状态planState为中止
        //查找排查计划执行状态为0待执行的数量
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,1);
        String tomorrow = timeFormat.format(cal.getTime());
        HtPlanDO planVO=htPlanMapper.selectById(createReqVO.getPlanId());
        if(planVO.getEndDate()!=null) {
            long d1 = timeFormat.parse(tomorrow).getTime();
            long d2 = planVO.getEndDate().getTime();
            if (d1==d2) {
                int stateNum = htPlanExeMapper.countPlanExeState(createReqVO.getPlanId(), createReqVO.getPlanExeId());
                if (stateNum == 0) {
                    HtPlanDO planDO = new HtPlanDO();
                    planDO.setId(createReqVO.getPlanId());
                    planDO.setPlanState("2");//设置计划状态为中止
                    htPlanMapper.updateById(planDO);
                }
            }
        }
        //处理排查计划执行结果图片
        List<OssDO> imgList=createReqVO.getOssImg();
        //批量插入隐患图片表信息
        for(OssDO ossDO:imgList){
            Long ossId=ossDO.getId();
            HtPlanDetailexeannexsDO entity= new HtPlanDetailexeannexsDO();
            entity.setPlanDetaiExeId(htPlanDetailExe.getId());
            entity.setOssId(ossId);
            entity.setCreateTime(ossDO.getCreateTime());
            entity.setCreator(ossDO.getCreator());
            entity.setUpdateTime(ossDO.getUpdateTime());
            entity.setUpdater(ossDO.getUpdater());
            htPlanDetailexeannexsMapper.insert(entity);
        }
        // 返回
        return htPlanDetailExe.getId();
    }

    @Override
    public void updateHtPlanDetailExe(HtPlanDetailExeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateHtPlanDetailExeExists(updateReqVO.getId());
        // 更新
        HtPlanDetailExeDO updateObj = HtPlanDetailExeConvert.INSTANCE.convert(updateReqVO);
        htPlanDetailExeMapper.updateById(updateObj);
    }

    @Override
    public void deleteHtPlanDetailExe(String id) {
        // 校验存在
        this.validateHtPlanDetailExeExists(id);
        // 删除
        htPlanDetailExeMapper.deleteById(id);
    }

    private void validateHtPlanDetailExeExists(String id) {
        if (htPlanDetailExeMapper.selectById(id) == null) {
            throw exception(HT_PLAN_DETAIL_EXE_NOT_EXISTS);
        }
    }

    @Override
    public HtPlanDetailExeDO getHtPlanDetailExe(String id) {
        return htPlanDetailExeMapper.selectById(id);
    }

    @Override
    public List<HtPlanDetailExeDO> getHtPlanDetailExeList(Collection<String> ids) {
        return htPlanDetailExeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HtPlanDetailExeDO> getHtPlanDetailExePage(HtPlanDetailExePageReqVO pageReqVO) {
        return htPlanDetailExeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HtPlanDetailExeDO> getHtPlanDetailExeList(HtPlanDetailExeExportReqVO exportReqVO) {
        return htPlanDetailExeMapper.selectList(exportReqVO);
    }

    @Override
    public List<OssDO> getImgList(String planDetailExeId) {
        List<String> ossIds= htPlanExeMapper.getOssIds(planDetailExeId);
       return  ossMapper.selectByIds(ossIds);
    }
}
